package com.wsc.wisdomchina.activitity.introduction.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.PageInfoDetailActivity;
import com.wsc.wisdomchina.activitity.introduction.LeaderBean;
import com.wsc.wisdomchina.activitity.introduction.LeaderListAdapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LeaderListFragment extends Fragment implements ContributorView {

    private PullToRefreshListView fragment_lyhd_listView;//列表
    private Context context;
    private ListView actualListView;
    private int skip = 0;
    private final int limit = 6;
    private boolean isfresh = true;
    private ContributorPresenter contributorPresenter;
    public static final String LEADER = "LEADER_LIST";
    private List<LeaderBean> leaderList;
    private LeaderListAdapter adapter;
    private String id;
    private String name;
    private RelativeLayout layout;
    private String idModule;


    public static LeaderListFragment newInstance(Bundle b) {
        LeaderListFragment fragment = new LeaderListFragment();
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        contributorPresenter = new ContributorPresenter(this, context);
        id = getArguments().getString("id");
        name = getArguments().getString("name");
        idModule = getArguments().getString("idmodule");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_town, container, false);
        initView(inflate);
        showData();
        return inflate;
    }

    /**
     * 初始化控件
     *
     * @param inflate
     */
    private void initView(View inflate) {
        fragment_lyhd_listView = (PullToRefreshListView) inflate.findViewById(R.id.fragment_lyhd_listView);//列表

        layout = (RelativeLayout) inflate.findViewById(R.id.layout);
        fragment_lyhd_listView.setMode(PullToRefreshBase.Mode.BOTH);
        actualListView = fragment_lyhd_listView.getRefreshableView();

        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        actualListView.setDivider(null);
        Tools.showRefreshView(fragment_lyhd_listView);

        fragment_lyhd_listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // TODO Auto-generated method stub
                // Update the LastUpdatedLabe
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showRefreshView(fragment_lyhd_listView);
                    skip = 0;
                    showData();
                } else {
                    isfresh = false;
                    Tools.showRefreshView(fragment_lyhd_listView);
                    skip += limit;
                    showData();
                }

            }
        });

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (leaderList != null && leaderList.size() > 0) {

                    Intent intent = new Intent(context, PageInfoDetailActivity.class);
                    intent.putExtra("id", LEADER);
                    intent.putExtra("actualId", leaderList.get(position - 1).getId());
                    intent.putExtra("name", name);
                    intent.putExtra("actualName", leaderList.get(position - 1).getLeaderName());
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * 请求展示数据
     */
    private void showData() {
        contributorPresenter.getLeaderList(id, skip, limit, LEADER);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(LEADER)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<LeaderBean> floorActivityListBeen = JsonTools.jsonStringToList(jsonMessage, LeaderBean.class);
                    if (floorActivityListBeen != null && floorActivityListBeen.size() > 0) {
                        if (isfresh) {
                            layout.setVisibility(View.GONE);
                            leaderList = floorActivityListBeen;
                            adapter = new LeaderListAdapter(context, floorActivityListBeen);
                            actualListView.setAdapter(adapter);
                        } else {
                            layout.setVisibility(View.GONE);
                            leaderList.addAll(floorActivityListBeen);
                            adapter.notifyDataSetChanged();
                        }

                    } else {
                        if (isfresh) {
                            layout.setVisibility(View.VISIBLE);
                        }
                    }

                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
        fragment_lyhd_listView.onRefreshComplete();
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

}
